<?php
namespace Emos\ListField;

use Emos\Common;

class String extends AbstractListField {    
    protected $maxLength;
    protected $formatFunc = null;
            
    function __construct($maxLength=50) {        
        $this->maxLength = $maxLength;
    }
    
    function getHtml() {    
        $val = $this->value;
        if(mb_strlen($val) > $this->maxLength) {            
            $html = Common::plain(trim(mb_substr($val, 0, $this->maxLength))).'<span class="dots">...</span>';
        }
        else {
            $html = Common::plain($val);
        }        
        
        if($this->formatFunc) {            
            $f = $this->formatFunc;
            $html = $f($html, $this->record);            
        }
        
        return $html;
    }    
    
    public function setFormatFunc($formatFunc) {
        $this->formatFunc = $formatFunc;
        return $this;
    }
}